import { _decorator, Component, Node, Collider, ICollisionEvent, Prefab, CCFloat, Vec2, Vec3 } from 'cc';
import { poolManager } from '../../../../../Framework/Scripts/Managers/poolManager';
import myUtil from '../../../../../Framework/Scripts/Utils/myUtil';
import { Vector3 } from '../../../../../Framework/Scripts/Utils/Vector3';
import { constant, playParticleOptions } from '../../../constant';

import { boss_aoeBomb } from '../../enemySkill/boss_aoeBomb';


const { ccclass, property } = _decorator;

/**
 * 炮弹
 */
@ccclass('Bullet')
export class Bullet extends Component {
    @property({ type: CCFloat, displayName: "伤害" })
    private hitNum: number = 100
    @property({ type: Prefab, displayName: "落地爆炸" })
    private boomNode: Prefab = null


    private howHit: Node = null

    private isTrigger: boolean = false

    private targetPos: Vec3 = null




    onLoad() {
        this.hitNum = this.hitNum + (this.hitNum * constant.buildData.cannonAttack.add * constant.buildData.cannonAttack.level)

    }

    init(howHit, targetPos) {
     

   
        this.howHit = howHit

        this.isTrigger = false
        this.targetPos = targetPos
        let options: playParticleOptions = { ndEffect: this.node };
        myUtil.playParticle2(options)
    }






    /**
     * 触发
     */
    private trigger() {


        let boom: Node = poolManager.instance.getNode(this.boomNode, this.node.parent.parent)

        boom.setWorldPosition(this.node.getWorldPosition())

        let ts: boss_aoeBomb = boom.getComponent(boss_aoeBomb)
        if (ts) {
            ts.hitNum = this.hitNum
            ts.init(this.howHit)
        }
        poolManager.instance.putNode(this.node);


    }

    update(dt: number) {
        if (this.isTrigger) {
            return
        }

        if (!this.targetPos) {
            //  console.log("目标没有了=================");
            return

        }

        const mypos = this.node.worldPosition
        const len: number = Vector3.sub(mypos, this.targetPos).lengthSqr()

        if (len < 4) {
            this.isTrigger = true
            //  console.log("爆炸", len);

            this.trigger()
        }

    }

}
